package com.android.baseconfig.common.helper;

import android.app.Activity;

import com.android.baseconfig.common.utils.Logc;

import java.util.Stack;

public class ActivityManager {
    private static Stack<Activity> mActivityStack = new Stack<Activity>();
    /**
     * 前台Activity
     */
    public static Activity ForegroundActivity;

    public static void addActivity(Activity activity) {
        mActivityStack.push(activity);
    }

    public static void removeActivity(Activity activity) {
        mActivityStack.remove(activity);
    }

    public static void removeActivity() {
        mActivityStack.pop();
    }

    public static void clear() {
        mActivityStack.clear();
    }

    public static Activity getActivity(int index) {
        if (mActivityStack.empty()) {
            return null;
        }
        return mActivityStack.get(index);
    }

    public static int getActivityCount() {
        return mActivityStack.size();
    }

    public static void finishAll() {
        for (Activity activity : mActivityStack) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
        mActivityStack.clear();
    }

    public static void finishAll(String exclude) {
        if (exclude == null) {
            exclude = "";
        }
        Activity excludeActivity = null;
        for (Activity activity : mActivityStack) {
            if (!activity.isFinishing()) {
                if (exclude.equalsIgnoreCase(activity.getClass().getSimpleName())) {
                    excludeActivity = activity;
                } else {
                    activity.finish();
                }
            }
        }
        mActivityStack.clear();
        if (excludeActivity != null) {
            mActivityStack.add(excludeActivity);
        }
    }

    public static Stack<Activity> getAllActivity() {
        return mActivityStack;
    }

    public static void toName() {
        if (mActivityStack.empty()) {
            Logc.i("toString: activity stack is empty");
            return;
        }
        for (int i = 0; i < mActivityStack.size(); i++) {
            Activity activity = mActivityStack.get(i);
            Logc.i("activity " + i +": " + activity.getClass().getSimpleName());
        }
    }
}

